<style lang="less" scoped>
  .page__wrapper {
    width: 100%;
    height: 100%;
  }
</style>

<template>
  <div class="page__wrapper">
    <simple-table :table-column="tableColumn" :table-data="tableData" :table-page-info="tablePageInfo" :table-data-count="tableDataTotalCount" @page-info-change="handlePageInfoChange">
      <template slot="searchSection">
        <simple-form v-model="searchModel" :form-field="searchFormField">
          <template slot="tableOperation">
            <el-button @click="handleDataSearch">{{$t('operation.search')}}</el-button>
            <el-button type="primary" v-if="actionList.indexOf('add') >= 0" @click="handleAddClick">{{$t('operation.create')}}</el-button>
          </template>
        </simple-form>
      </template>
      <template slot="operation" slot-scope="scope">
        <el-button circle icon="el-icon-search" :title="$t('operation.view')" v-if="actionList.indexOf('view') >= 0" @click="handleViewClick(scope.row)"></el-button>
        <el-button circle icon="el-icon-edit" :title="$t('operation.edit')" v-if="actionList.indexOf('edit') >= 0" @click="handleEditClick(scope.row)"></el-button>
        <el-button circle icon="el-icon-delete" :title="$t('operation.delete')" v-if="actionList.indexOf('del') >= 0" @click="deleteRecord(scope.row)"></el-button>
      </template>
    </simple-table>
    <detail-dialog :show-flag.sync="isShowDetailDialog" :detail-data="rowData" :view-flag="isViewFlag" :ext-data="{sys_mid: sys_mid}"></detail-dialog>
  </div>
</template>
<script>
/** 混入分页信息及数据总数信息 */
import pageMixins from '@/components/base/simple-table/page-mixins'
import detailDialog from './detail'
import Axios from 'axios'
const BASEURL = {
  list: '/web/SysAdmin/ajax.html?sys_method=GetList&sys_objName=Config_KeyValue&sys_tableName=Config_KeyValue',
  getActions: '/Web/SysAdmin/ajax.html?sys_method=GetActions&sys_objName=Sys_Action',
  getInitConfig: '/Web/SysAdmin/ajax.html?sys_method=GetInitConfig',
  delete: '/web/SysAdmin/ajax.html?sys_method=Delete&sys_objName=Config_KeyValue'
}
export default {
  name: 'system_dict',
  mixins: [pageMixins],
  components: { detailDialog },
  data () {
    return {
      searchModel: {},
      /** 表格数据 */
      tableData: [],
      rowData: {},
      /** 是否显示详情页 dialog */
      isShowDetailDialog: false,
      /** 是否为查看数据 */
      isViewFlag: false,
      actionList: [],
      sys_mid: ''
    }
  },
  computed: {
    searchFormField () {
      return [
        { prop: 'configkey', label: this.$t('system_dict.configkey'), type: 'Input', component: { clearable: true } },
        { prop: 'configname', label: this.$t('system_dict.configname'), type: 'Input', component: { clearable: true } },
        { prop: 'configvalue', label: this.$t('system_dict.configvalue'), type: 'Input', component: { clearable: true } },
        { slotName: 'tableOperation', col: { span: 6, style: { textAlign: 'right' } }, labelWidth: '0' }
      ]
    },
    tableColumn () {
      return [
        { label: this.$t('system_dict.configkey'), prop: 'configkey', width: 200 },
        { label: this.$t('system_dict.configname'), prop: 'configname', width: 200 },
        { label: this.$t('system_dict.configvalue'), prop: 'configvalue', width: 200 },
        { label: this.$t('system_dict.orderno'), prop: 'orderno', width: 100 },
        { label: this.$t('system_dict.createtime'), prop: 'createtime', width: 200 },
        { label: this.$t('system_dict.notes'), prop: 'notes', minWidth: 240 },
        { label: this.$t('field.operation'), slotName: 'operation', width: 120, fixed: 'right' }
      ]
    }
  },
  /**
   * 注入到子组件
   */
  provide () {
    return { handleDataSearch: this.handleDataSearch }
  },
  methods: {
    handleDataSearch () {
      let loadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })
      let params = Object.assign({}, this.tablePageInfo, { sys_mid: this.sys_mid, sort: 'createtime', order: 'desc' }, { sys_search: this.searchModel })
      this.$axios
        .post(BASEURL.list, params)
        .then(resp => {
          this.tableData = resp.rows
          this.tableDataTotalCount = resp.total
        })
        .finally(() => {
          loadingFlag.close()
        })
    },
    handleInitData () {
      let configLoadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })
      let params = Object.assign({}, this.tablePageInfo, { sys_mid: this.sys_mid, sort: 'createtime', order: 'desc' }, { sys_search: this.searchModel })
      this.$axios
        .post(BASEURL.getInitConfig, {})
        .then(resp => {
          // 设置 actionList及sys_mid
          this.actionList = resp.actionKeys.split(',')
          this.sys_mid = resp.mid
          let loadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })
          Axios.all([
            this.$axios.post(BASEURL.list, params),
            this.$axios.post(BASEURL.getActions, {})
          ])
            .then(Axios.spread((dictData, actionData) => {
              this.actionsData = actionData
              this.tableData = dictData.rows
              this.tableDataTotalCount = dictData.total
            }))
            .finally(() => {
              loadingFlag.close()
            })
        })
        .finally(() => {
          configLoadingFlag.close()
        })
    },
    handleAddClick () {
      this.rowData = {}
      this.isViewFlag = false
      this.isShowDetailDialog = true
    },
    handleViewClick (row) {
      this.rowData = this.$_.cloneDeep(row)
      this.isViewFlag = true
      this.isShowDetailDialog = true
    },
    handleEditClick (row) {
      this.rowData = this.$_.cloneDeep(row)
      this.isViewFlag = false
      this.isShowDetailDialog = true
    },
    deleteRecord (row) {
      this.$msgbox({
        title: this.$t('tip.warning'),
        message: this.$t('tip.confirmDelete'),
        type: 'warning',
        showCancelButton: true,
        closeOnClickModal: false,
        closeOnPressEscape: false,
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            this.$axios
              .post(BASEURL.delete, { id: row.keyvalueid, idField: 'keyvalueid', sys_mid: this.sys_mid })
              .then(resp => {
                this.$message({ type: 'success', message: this.$t('tip.deleteSuccess') })
                done()
              })
              .finally(() => {
                instance.confirmButtonLoading = false
              })
          } else done()
        }
      })
        .then(() => {
          this.handleDataSearch()
        })
    }
  },
  mounted () {
    this.handleInitData()
  }
}
</script>
